import { ctrlInitFun } from './../dataSetUtils/ctrl';
import { allInitFun } from './../dataSetUtils/all';
import { drawLeafletInitFun } from './../dataSetUtils/drawLeaflet';
// js dist
import L from './../libs/leaflet1.9.4';
// ===================== 已经封装融合成功过 ======================
import './../libs/Leaflet.TrackPlayBack';
import './../libs/Semicircle';
import './../libs/Leaflet.Draw/leaflet.draw-src.js';

// 轨迹编辑
import './../libs/L.Polyline.PolylineEditor/L.Polyline.PolylineEditor.js';
import './../libs/L.Polyline.PolylineEditor/Leaflet.Editable.js';

// ====================== 尚未封装融合成功过 =====================
// ====================== utils ======================

// css dist
import './../libs/leaflet1.9.4/dist/leaflet.css';
import './../libs/Leaflet.Draw/leaflet.draw-src.css';

import '../libs/turf.min.js'; // https turf.js

const LFTCanvas = function (mapEle, options) {
  const defaultOptions = {
    preferCanvas: true,
    contextmenu: true,
    crs: L.CRS.EPSG3395,
    center: options.center || [51.505, -0.09],
    zoom: options.center || 7,
    editable: true, // 可编辑
  };
  let mOptions = Object.assign({ ...defaultOptions }, { ...options });
  this._map = L.map(mapEle, mOptions).setView(mOptions.center, mOptions.zoom);
  this._groups = {};
  this._layers = {};
  this._L = L;
};

LFTCanvas.prototype._LFTDestory = LFTCanvas.prototype.remove;
LFTCanvas.prototype = {
  setLFTCtrlData: function (data, cb) {
    ctrlInitFun(data, this, cb);
  },

  setLFTAllData: function (data, cb) {
    allInitFun(data, this, cb);
  },

  setLFTDrawData: function (data, cb) {
    drawLeafletInitFun(data, this, cb);
  },

  flyToPosition: function (lat, lng, zoom) {
    this._map.flyTo([lat, lng], zoom);
  },

  setZoom: function (zoom) {
    this._map.setZoom(zoom);
  },

  setView: function (lat, lng, zoom) {
    this._map.setView([lat, lng], zoom);
  },

  getZoom: function () {
    return this._map.getZoom();
  },

  getCenter: function () {
    return this._map.getCenter();
  },

  getBounds: function () {
    return this._map.getBounds();
  },

  getBoundsZoom: function (bounds) {
    return this._map.getBoundsZoom(bounds);
  },
  // 添加组
  addLFTLayerGroup: function (layerGroup) {
    if (!Object(this._groups).hasOwnProperty(layerGroup.id)) {
      this._groups[layerGroup.id] = layerGroup;
    }
    return this;
  },
  // 获取组
  getLFTLayerGroup: function (id) {
    if (Object(this._groups).hasOwnProperty(id)) {
      return this._groups[id];
    }
    return null;
  },
  // 删除组
  removeLFTLayerGroup: function (id) {
    if (Object(this._groups).hasOwnProperty(id)) {
      // 此处调用构造函数的方法
      this._groups[id].remove();
    }
  },
  // 清空缓存组
  removeLFTLayerGroupInCache: function (id) {
    if (Object(this._groups).hasOwnProperty(id)) {
      delete this._groups[id];
    }
  },
  // 添加图层
  addLFTLayer: function (layer) {
    if (!Object(this._layers).hasOwnProperty(layer.id)) {
      this._layers[layer.id] = layer;
    }
    return this;
  },
  // 清除图层
  removeLFTLayer: function (id) {
    console.log(this._layers[id]);
    if (this._layers[id].groupId) {
      let gpID = this._layers[id].groupId;
      let layers = Object(this._groups).hasOwnProperty(gpID)._layers;
      if (layers && Object(layers).hasOwnProperty(id)) {
        delete Object(this._groups)[gpID]._layers[id];
      }
    }
    if (Object(this._layers).hasOwnProperty(id)) {
      this._layers[id].removeLayer(id);
    }
  },
  // 清空缓存图层
  removeLFTLayerInCache: function (id) {
    let groups = this._groups;
    for (let key in groups) {
      let _group = groups[key];
      if (Object(_group._layers).hasOwnProperty(id)) {
        delete _group._layers[id];
      }
    }
  },
  // 根据组id获取图层
  getLFTLayers: function (groupId) {
    if (groupId) {
      let _group = this.getLFTLayerGroup(groupId);
      if (_group) {
        return _group ? _group.getLayers() : {};
      }
    } else {
      let _tmp = Object.values(this._groups)
        .map((m) => m._layers)
        .flat();
      if (_tmp.length > 0) {
        return _tmp.reduce((a, b) => {
          return { ...a, ...b };
        });
      }
    }
  },
  // 根据组id 层id获取层
  getLFTLayer: function (layerId, groupId) {
    let _layer;
    if (layerId) {
      if (groupId) {
        let _group = this.getLFTLayerGroup(groupId);
        _layer = _group ? _group.getLayer(layerId) : null;
      } else {
        let _tmp = Object.values(this._groups)
          .map((m) => m._layers)
          .flat();
        if (_tmp.length > 0) {
          _layer = _tmp.reduce((a, b) => {
            return { ...a, ...b };
          })[layerId];
        }
      }
    }
    return _layer;
  },
  // 根据组id 层id获取overlays
  getAllOverlays: function (layerId, groupId) {
    let _layers = this.getLFTLayers(groupId);
    let _overlays = {};
    if (_layers[layerId]) {
      _overlays = _layers[layerId]._overlays;
    } else {
      let _tmp = Object.values(_layers)
        .map((m) => m._overlays)
        .flat();
      if (_tmp.length > 0) {
        _overlays = _tmp.reduce((a, b) => {
          return { ...a, ...b };
        });
      }
    }
    return _overlays;
  },
  // 根据图层组id 图层id获取覆盖物
  getOverlays: function (layerId, groupId, ids) {
    let _layers = this.getLFTLayers(groupId);
    let _overlays = {};
    if (_layers[layerId]) {
      _overlays = _layers[layerId]._overlays;
    } else {
      let _tmp = Object.values(_layers)
        .map((m) => m._overlays)
        .flat();
      if (_tmp.length > 0) {
        _overlays = _tmp.reduce((a, b) => {
          return { ...a, ...b };
        });
      }
    }
    if (ids) {
      let _arr = [];
      let _tmp = {};
      if (typeof ids === 'string') {
        _arr = ids.split(',');
      } else if (Array.isArray(ids)) {
        _arr = ids;
      }
      _arr.forEach((id) => {
        if (_overlays[id]) {
          _tmp[id] = _overlays[id];
        }
      });
      return _tmp;
    } else {
      return _overlays;
    }
  },
  // 获取单个overlay
  getOverlay: function (overlayId, groupId, layerId) {
    let _layer;
    let _overlay = null;
    if (layerId) {
      if (groupId) {
        let _group = this.getLFTLayerGroup(groupId);
        _layer = _group ? _group.getLayer(layerId) : null;
      }
    }
    if (overlayId) {
      _overlay = _layer ? _layer.getOverlays(overlayId) : null;
    }
    if (!layerId && !groupId) {
      let _tmp = Object.values(this._groups)
        .map((m) => Object.values(m._layers).map((n) => n._overlays))
        .flat();
      if (_tmp.length > 0) {
        _overlay = _tmp.reduce((a, b) => {
          return { ...a, ...b };
        })[overlayId];
      }
    }
    return _overlay;
  },

  // 绑定leaflet map的事件
  on: function (type, fn, context) {
    this._map.on(type, fn, context);
    return this;
  },
  // 卸载map 事件
  off: function (type, fn, context) {
    this._map.off(type, fn, context);
    return this;
  },

  // 通过pixijs创建一个canvas

  // 对原函数进行增强写法
  destory: function () {
    // 删除所有group
    Object.values(this._groups).forEach((m) => m.remove());
    this._LFTDestory();
  },
};

export default LFTCanvas;
